﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Team6_Algorithms.Operations;
using Team6_Algorithms.AidingClasses;
using Team6_Algorithms.Binary;
using System.Drawing.Imaging;

namespace Team6_Algorithms.OCR
{
    /// <summary>
    /// Made by Pascal Schotman && Dylan Snel
    /// </summary>
    [Serializable()]
    public class SingleCharacter 
    {
        public string Name, Character;
        public float[] Score;
        public int PixelCount = 0;
        public int tl = 0, tr = 0, bl = 0, br = 0;
        public Bitmap Image;
        public int Width, Height;
        public PixelPoint[,] Binary;
        
        public int xMin, xMax = 0, yMin, yMax = 0;

        public SingleCharacter(Bitmap srcBitmap, string name)
        {
            Name = name;
            Character = Name.Substring(Name.IndexOf('_') +1, 1);
            
            readCharacter(srcBitmap);
            Console.WriteLine("Lezen van " + Name + " Character is " + Character+ " Assigned Score = "+Score);
        }

        public void readCharacter(Bitmap srcBitmap)
        {
            byte[] rgbValues;
            Image = srcBitmap;
            Binary = (PixelPoint[,])Binaryze.InvertBinary(Image);
            Image = Binaryze.PaintInvertedBinairy(Image, Binary);
            
            Rectangle rect = new Rectangle(0, 0, Image.Width, Image.Height);
            BitmapData bmpData = Image.LockBits(rect, ImageLockMode.ReadWrite, Image.PixelFormat);
            IntPtr bmpPtr = bmpData.Scan0;

            int bytes = (Image.Width * Image.Height) * 3;
            rgbValues = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(bmpPtr, rgbValues, 0, rgbValues.Length);

            Color black = Color.FromArgb(0,0,0);
            xMin = Image.Width;
            yMin = Image.Height;
            for (int x = 0; x < Image.Width; x++)
            {
                for (int y = 0; y < Image.Height; y++)
                {
                    int index = ((y * Image.Width + x) * 3);
                    int b = rgbValues[index];
                    int g = rgbValues[index + 1];
                    int r = rgbValues[index + 2];
                    Color temp = Color.FromArgb(r, g, b);
                    if (temp == black)
                    {
                        if (x < Image.Width / 2)
                        {
                            if (y < Image.Height / 2)
                            {
                                tl++;
                            }
                            else
                            {
                                tr++;
                            }
                        }
                        else
                        {
                            if (y < Image.Height / 2)
                            {
                                bl++;
                            }
                            else
                            {
                                br++;
                            }
                        }
                            PixelCount++;
                        if (x < xMin)
                            xMin = x;
                        if (y < yMin)
                            yMin = y;
                        if (x > xMax)
                            xMax = x;
                        if (y > yMax)
                            yMax = y;
                    }
                }
            }

            
            Image.UnlockBits(bmpData);
            Console.WriteLine("Gaat nu Croppen");
            this.Image = Image.crop(xMin, xMax, yMin, yMax);
            this.Width = Image.Width;
            this.Height = Image.Height;
            assignScore();
        }

        public void assignScore()
        {
            Score = new float[5];
            float WHRatioFactor = 2.0f;
            float PixelRatioFactor = 2.0f;

            float WHRatio = (Width * 10000) / (Height * 10000);
            int Quarter = (Width * Height) /4;
          


            float PixelRatio = ((Width * Height) * 10000) / PixelCount;

            Score[0] = (PixelRatio * PixelRatioFactor) + (WHRatio * WHRatioFactor);
            Score[1] = (tl * 1000) / Quarter;
            Score[2] = (tr * 1000) / Quarter;
            Score[3] = (bl * 1000) / Quarter;
            Score[4] = (br * 1000) / Quarter;


        }

       public float compareScore(float[] ExScore)
        {
            float ScoreDiff = 0.0f;
            ScoreDiff += Math.Abs(ExScore[1] - Score[1]);
            ScoreDiff += Math.Abs(ExScore[2] - Score[2]);
            ScoreDiff += Math.Abs(ExScore[3] - Score[3]);
            ScoreDiff += Math.Abs(ExScore[4] - Score[4]);
            ScoreDiff *= 1000;
            ScoreDiff += Math.Abs(ExScore[0] - Score[0]);
            return ScoreDiff;
        }
    }
}
